package xdu.lz.stage6_threadpool.chapter1_threadPoolExecutor;

import java.util.concurrent.*;

/**
 * Created at 2019/10/24 0024 上午 11:07
 * --
 */
public class TestRejected {

    public static void main(String[] args) throws InterruptedException {
       // buildThread(new ThreadPoolExecutor.AbortPolicy());
        //buildThread(new ThreadPoolExecutor.DiscardPolicy());
        //main run the task3
//        buildThread(new ThreadPoolExecutor.CallerRunsPolicy());
        //task2 is discard,because it is the oldest task in the queue
        buildThread(new ThreadPoolExecutor.DiscardOldestPolicy());
    }
    private static ThreadPoolExecutor buildThread(RejectedExecutionHandler reject) throws InterruptedException {
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(1, 1, 200, TimeUnit.SECONDS,
                        new ArrayBlockingQueue<>(1),
                        Executors.defaultThreadFactory(),
                        reject);

        executor.submit(() -> {
            System.out.println("task1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.submit(() -> {
            System.out.println("task2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread.sleep(100);
        executor.submit(()->{
            System.out.println(Thread.currentThread().getName() + " run the task3 ");
        });
        return executor;
    }

}
